package com.wang.myblog.controller;

import com.alibaba.fastjson.JSONObject;
import com.wang.myblog.common.PageRequest;
import com.wang.myblog.common.PageResponse;
import com.wang.myblog.common.ResponseResult;
import com.wang.myblog.entity.Article;
import com.wang.myblog.entity.Comment;
import com.wang.myblog.service.ArticleService;
import com.wang.myblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleService articleService;

    /**
     * 前往评论管理界面
     */
    @RequestMapping("/commentManage")
    public String commentManage(){
        return "admin/commentManage";
    }

    /**
     * 获取评论信息
     */
    @PostMapping("/commentManageInfo")
    @ResponseBody
    public PageResponse commentManageInfo(@RequestBody PageRequest request){
        request.setSearchMsg(request.getSearchMsg().replace(" ",""));
        if (request.getSearchMsg().equals(""))
            request.setSearchMsg(null);
        PageResponse pageResponse=commentService.queryByType(request.getDraw(),request.getOffset(), request.getPageSize(), 0, request.getSearchMsg());
        return pageResponse;
    }

    /**
     * 添加评论
     */
    @PostMapping("/addComment")
    @ResponseBody
    public String addComment(Comment comment,HttpSession session){
        ResponseResult result=new ResponseResult();
        comment.setType(0);
        comment.setTime(new Date());
        comment.setStatus(0);
        comment.setIp(null);
        if (commentService.insert(comment)) {
            session.setAttribute("unReadCount",commentService.getUnReadCount());
            session.setAttribute("unReadComment",commentService.queryUnRead(0,4));
            result.setCode(200);
        }else {
            result.setCode(500);
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * 前往阅读评论界面
     */

    @GetMapping("/readComment")
    public String readComment(Map<String,Object> map, @RequestParam("id") Integer id, HttpSession session){
        Comment comment = commentService.queryById(id);
        comment.setStatus(1);
        commentService.update(comment);
        map.put("comment",comment);
        // 获取未读评论数
        map.put("unRead",commentService.getUnReadCount());
        session.setAttribute("unReadCount",commentService.getUnReadCount());
        session.setAttribute("unReadComment",commentService.queryUnRead(0,4));
        return "admin/readComment";
    }

    /**
     * 回复评论
     */
    @PostMapping("/replyComment")
    @ResponseBody
    public String replyComment(Comment comment){
        // 获取被回复的留言
        Comment reply = commentService.queryById(comment.getReplyId());
        comment.setTime(new Date());
        comment.setType(1);
        comment.setArticleId(reply.getArticleId());
        ResponseResult result=new ResponseResult();
        if (commentService.insert(comment)) {
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("回复失败");
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * 删除评论
     */
    @GetMapping("/deleteComment")
    @ResponseBody
    public String deleteComment(@RequestParam("id") Integer id,HttpSession session){
        Comment comment = commentService.queryById(id);
        ResponseResult result=new ResponseResult();
        if (commentService.deleteById(id)) {
            commentService.deleteByReplyId(id);
            session.setAttribute("unReadCount",commentService.getUnReadCount());
            session.setAttribute("unReadComment",commentService.queryUnRead(0,4));
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("删除失败");
        }
        return JSONObject.toJSONString(result);
    }

}
